<%--
  Created by IntelliJ IDEA.
  User: fengdefangxiang
  Date: 2022/2/8
  Time: 22:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@ include file="/web/header.jsp"%>
    <title>上传图片</title>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin:20px" >
    <legend>头像图片</legend>
    <div class="layui-field-box">
        <button type="button" class="layui-btn" id="test1">
            <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
        <table class="layui-table" >
            <thead>
            <tr>
                <td>文件名</td>
                <td>图片</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
    </div>
</fieldset>


<script type="text/javascript">
    var code = '<%=request.getParameter("code")%>';
    renderUpload("test1","/EmployeeUploadServlet",{code:code},function(res){
        if(res.code == 1){
            layer.msg("上传成功");
            showPic(res.image)
        }
    })
    function init(){
        ajax('/EmployeeServlet',{action:'get',code:code},'json',function(data){
            console.log(data)
            var image = data.image;
            if(image){
                showPic(image);
            }else{
                $("tbody").html('');
            }
        })
    }
    init();
    function showPic(image){
        var html =
            "<tr><td>"+image+"</td>"+
            "<td><img src='/img/" + image + "' class='layui-upload-img'></td>"+
            "<td><input type='button' onclick='delPic()' class='layui-btn' value='删除'></td>"+
            "</tr>"
        $("tbody").html(html)
    }
    function delPic(){
        layerConfig(function(index) {
            ajax("/EmployeeServlet", {
                code : code,
                action : 'delPic'
            }, 'text', function(data) {

                if (data == 1) {
                    layer.msg("删除成功");
                } else {
                    layer.msg("删除失败");
                }
                init();
            });
        })
    }

</script>
</body>
</html>